import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

import Dialog from '/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buyerName: '',
    buyerPhone: '',
    address: '',
    addressDetial: '',
    buyerOpenid: '',
    orderAmount: 0,
    orderDetailList: [],
    productNum: 0,
    sumPrice: 0,
    show: false,
    orderId: '',
    createTime:'',
    orderNum:''
  },

  /**
   * 支付订单
   */
  submitOrder() {
    Dialog.confirm({
      title: '订单支付',
      message: '是否支付订单',
      cancelButtonText: '下次再说',
      confirmButtonText: '立即支付',
      confirmButtonColor: '#07c160'
    }).then(() => {
      this.setData({
        show: true
      })

    }).catch(() => {
      // on cancel
      wx.showToast({
        title: '订单取消',
        icon: 'none'
      });
    });
  },
  failSubmitOrderApi() {
    if (this.data.orderId != "" && this.data.orderId != null) {
      const data = {
        orderId: this.data.orderId,
        buyerName: this.data.buyerName,
        buyerPhone: this.data.buyerPhone,
        buyerAddress: this.data.address + this.data.addressDetial,
        payStatus: '0',
      }
      this.restXiadan(data)
    } else {
      const data = {
        buyerName: this.data.buyerName,
        buyerPhone: this.data.buyerPhone,
        buyerAddress: this.data.address + this.data.addressDetial,
        buyerOpenid: wx.getStorageSync('openId'),
        orderAmount: this.data.sumPrice / 100,
        payStatus: '0',
        orderStatus: '1',
        orderDetailList: this.data.orderDetailList

      }
      http.request({
        url: 'order-master/xiaDanOrder',
        method: 'POST',
        data: data,
        success: (res) => {
          this.setData({
            show: false
          })
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          });

        }
      })
    }

  },
  submitOrderApi() {

    if (this.data.orderId != "" && this.data.orderId != null) {
      const data = {
        orderId: this.data.orderId,
        buyerName: this.data.buyerName,
        buyerPhone: this.data.buyerPhone,
        buyerAddress: this.data.address + this.data.addressDetial,
        payStatus: '1',
      }
      this.restXiadan(data)
    } else {
      const data = {
        buyerName: this.data.buyerName,
        buyerPhone: this.data.buyerPhone,
        buyerAddress: this.data.address + this.data.addressDetial,
        buyerOpenid: wx.getStorageSync('openId'),
        orderAmount: this.data.sumPrice / 100,
        payStatus: '1',
        orderStatus: '1',
        orderDetailList: this.data.orderDetailList

      }
      http.request({
        url: 'order-master/xiaDanOrder',
        method: 'POST',
        data: data,
        success: (res) => {
          console.log(res)
          this.setData({
            show: false
          })
          wx.showToast({
            title: '支付成功,跳转到订单',
            icon: 'none'
          });
          setTimeout(function () {
            wx.navigateTo({ url: '/pages/order/order?active=one' })
          }, 1000);

        }
      })
    }
  },

  updateValue(e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  /**
   * 查询订单
   */
  queryOrderInfo(orderId) {
    http.request({
      url: 'order-master/' + orderId,
      method: 'get',
      success: (res) => {
        const data = res.data
        this.setData({
          buyerName: data.buyerName,
          buyerPhone: data.buyerPhone,
          address: data.buyerAddress,
          sumPrice: data.orderAmount * 100,
          orderId: data.orderId,
          orderNum:data.orderNum,
          createTime: data.createTime,
          orderDetailList: data.orderDetailList
        })
      }
    })

  },
  restXiadan(data) {
    http.request({
      url: 'order-master/restXiadan',
      method: 'post',
      data: data,
      success: (res) => {
        this.setData({
          show: false
        })
        if (data.payStatus == 0) {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '支付成功，跳转到订单',
            icon: 'none'
          });
          setTimeout(function () {
            wx.navigateTo({ url: '/pages/order/order?active=one' })
          }, 1000);

        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.queryOrderInfo(options.orderId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})